package com.ruoyi.yuanliao.controller;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.yuanliao.domain.TraceTransport;
import com.ruoyi.yuanliao.domain.TraceUserCrops;
import com.ruoyi.yuanliao.service.FarmerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: blockchain-fabric-group1
 * @package: com.ruoyi.yuanliao.controller
 * @className: FarmerController
 * @author: 刘乘滨
 * @description: TODO
 * @date: 2023/05/21 20:13
 * @version: 1.0
 */
@RestController
@RequestMapping("/changshang/farmer")
public class FarmerController {

    @Autowired
    FarmerService farmerService;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * @author wx
     * @Date  2020/11/3 0003
     * @Param [deptId]获取部门下的司机
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/getAllDriverByDeptId/{deptId}")
    public List<SysUser> getAllDriverByDeptId(@PathVariable("deptId") String deptId){
        return farmerService.getAllDriverByDeptId(deptId);
    }

    @GetMapping("/getFactoryByDeptId/{deptId}")
    public List<SysDept> getFactoryByDeptId(@PathVariable("deptId") Integer deptId){
        return farmerService.getFactoryByDeptId(deptId);
    }

    @PostMapping("/addTransport")
    public int addTransport(@RequestBody TraceTransport traceTransport){
        return farmerService.addTransport(traceTransport);
    }


    /**
     * 添加作物
     *
     * @param traceCrops
     * @return
     */
    @PostMapping("/saveCrops")
    public Map<String,Object> saveCrops(@RequestBody TraceUserCrops traceCrops) {
        HashMap<String, Object> map = new HashMap<>();
        traceCrops.setUsername(SecurityUtils.getUsername());

        if(redisTemplate.hasKey(traceCrops)){
            map.put("flag",false);
            map.put("msg","表单重复提价，请过段时间再来");
            return map;
        }
        redisTemplate.opsForValue().set(traceCrops,traceCrops,5, TimeUnit.MINUTES);

        int i = farmerService.saveCrops(traceCrops);

        if(i > 0){
            map.put("flag",true);
            map.put("msg","添加成功");
        }else{
            map.put("flag",false);
            map.put("msg","添加失败");
        }
        return map;
    }

    /**
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author steakliu
     * @Date 23:12 2020/10/27 0027
     * @Param [username]
     **/
    @GetMapping("/getCropsByUsername/{username}")
    public List<TraceUserCrops> getCropsByUsername(@PathVariable("username") String username) {
        return farmerService.getCropsByUsername(username);
    }

}
